package com.ado.abyss.core.web.aspect;

import javax.servlet.http.HttpServletRequest;

import com.ado.abyss.core.web.utils.HttpUtils;
import com.ado.abyss.core.web.utils.IPUtils;
import com.ado.abyss.core.web.utils.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.ado.abyss.core.dal.model.SysLog;
import com.ado.abyss.core.service.SysLogService;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalDateTime;
import java.util.Date;

/***
 * 
 * @author adonais
 * @date 2020/05/20
 * @description 系统日志，切面处理类，记录日志
 *
 */
@Aspect
@Component	
public class SysLogAspect {

	@Autowired
	@Qualifier(value = "sysLogService")
	private SysLogService sysLogService;
	
	@Pointcut("execution(* com.ado.abyss.core.service.*.*(..))")
	public void logPointCut() { 
		
	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		// 执行方法
		Object result = point.proceed();
		// 执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;
		// 保存日志
		saveSysLog(point, time);
		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		String userName = SecurityUtils.getUsername();
		if(joinPoint.getTarget() instanceof SysLogService) {
			return ;
		}
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		SysLog sysLog = new SysLog();

		// 请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");

		// 请求的参数
		Object[] args = joinPoint.getArgs();
		try{
			String params = JSONObject.toJSONString(args[0]);
			if(params.length() > 200) {
				params = params.substring(0, 200) + "...";
			}
			sysLog.setParams(params);
		} catch (Exception e){
		}

		// 获取request
		HttpServletRequest request = HttpUtils.getHttpServletRequest();
		// 设置IP地址
		sysLog.setIp(IPUtils.getIpAddr(request));

		// 用户名
		sysLog.setUserName(userName);
		
		// 执行时长(毫秒)
		sysLog.setTime(time);

		//完善日志相关库中记录
		sysLog.setCreateTime(new Date());
		sysLog.setCreateBy(userName);
		sysLog.setLastUpdateTime(new Date());
		sysLog.setLastUpdateBy(userName);
		
		// 保存系统日志
		sysLogService.save(sysLog);
	}
}
